# == Schema Information
#
# Table name: micoposts
#
#  id            :bigint(8)        not null, primary key
#  content       :text(65535)
#  status        :integer
#  created_at    :datetime         not null
#  updated_at    :datetime         not null
#  user_id       :bigint(8)
#  like_count    :integer
#  collect_count :integer
#  share_count   :integer
#

class Micopost < ApplicationRecord
  belongs_to :user
  has_many :parises
  validates_presence_of :user
  validates :content, length: {maximum: 140}

  enum status: {active: 1, forbidden: 0}
  default_value_for :status, 1

  scope :sorted, -> { order(created_at: :desc) }

  def post_by_user(user)
    self.user = user
    self.save
    #todo 发送通知给关注者
  end

  def parises_num
    Parise.where(micopost: self).count
  end

  def comments_num

  end

  def shares_num

  end

  def can_parise(user)
    if Parise.where(user: user).where(micopost: self).count > 0
      false
    else
      true
    end
  end


end
